package com.yidu.common.domain;

import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.esotericsoftware.reflectasm.ConstructorAccess;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.yidu.common.bean.Snowflake;
import lombok.Data;

import java.io.Serializable;
import java.util.Date;

@Data
public class BaseEntity implements Serializable
{
    public static final int DELETED = 0;

    @TableId
    private Long id;
    @JsonInclude
    @TableLogic(value = "0", delval = "1")
    private Integer deleted;
    private Date createTime;
    @JsonInclude
    private Date updateTime;

    private transient int page = 1;

    private transient int size = 10;

    public static <E extends BaseEntity> E initEntity(Class<E> clazz)
    {
        E entity = ConstructorAccess.get(clazz).newInstance();
        Date now = new Date();
        entity.setDeleted(0);
        entity.setCreateTime(now);
        entity.setUpdateTime(now);
        return entity;
    }

    public static void initEntity(BaseEntity entity, Snowflake snowflake)
    {
        Date now = new Date();
        entity.setId(snowflake.nextId());
        entity.setDeleted(0);
        entity.setCreateTime(now);
        entity.setUpdateTime(now);
    }

    public static void initEntity(BaseEntity entity)
    {
        Date now = new Date();
        entity.setDeleted(0);
        entity.setCreateTime(now);
        entity.setUpdateTime(now);
    }
}
